package com.fg.pro.trade.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fg.pro.model.trade.dto.GmQuotationDto;
import com.fg.pro.trade.dao.TradeQuotationDao;
import com.fg.pro.trade.entity.TradeGoods;
import com.fg.pro.trade.entity.TradeQuotation;
import com.fg.pro.trade.service.TradeQuotationService;
import com.fg.pro.trade.utils.GmHttpUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import static com.fg.pro.trade.utils.GmRequestUrl.GM_QUOTATION_URL;
import static com.fg.pro.trade.utils.GmRequestUrl.REDIS_LOCK_URL;

/**
 * 报价单(TradeQuotation)表服务实现类
 *
 * @author makejava
 * @since 2025-08-14 09:36:13
 */
@Service("tradeQuotationService")
public class TradeQuotationServiceImpl extends ServiceImpl<TradeQuotationDao, TradeQuotation> implements TradeQuotationService {

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    GmHttpUtils gmHttpUtils;

    @Override
    public PageInfo getPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TradeQuotation> list = query().list();
        PageInfo<TradeQuotation> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public boolean refresh(Date date) {
        RLock lock = redissonClient.getLock(REDIS_LOCK_URL+GM_QUOTATION_URL);
        boolean tryLock = lock.tryLock();
        if(!tryLock){
            return false;
        }
        try {

            return refreshTradeQuotationByLock(date);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    @Transactional
    public boolean refreshTradeQuotationByLock (Date date) throws InterruptedException {

        GmQuotationDto.GmQuotationReponse gmQuotationReponse = gmHttpUtils.httpPostByToken(GM_QUOTATION_URL, new GmQuotationDto().new GmQuotationRequest().gmQuotationRequest(999), GmQuotationDto.GmQuotationReponse.class);
        LinkedList<TradeQuotation> newList = new LinkedList<>();
        for (GmQuotationDto.GmQuotationReponse.GmQuotationData gmQuotationData : gmQuotationReponse.getQuotations()) {
            TradeQuotation tradeQuotation = new TradeQuotation();
            tradeQuotation.setId(gmQuotationData.getQuotation_id());
            tradeQuotation.setCreateTime(new Date(gmQuotationData.getCreate_time()));
            tradeQuotation.setUpdateTime(new Date(gmQuotationData.getUpdate_time()));
            tradeQuotation.setQuotationGroupId(gmQuotationData.getGroup_id());
            tradeQuotation.setQuotationInnerName(gmQuotationData.getInner_name());
            tradeQuotation.setQuotationOuterName(gmQuotationData.getOuter_name());
            tradeQuotation.setQuotationType(gmQuotationData.getType());
            tradeQuotation.setQuotationSkuCount(gmQuotationData.getSku_count());
            tradeQuotation.setQuotationCustomerCount(gmQuotationData.getCustomer_count());
            tradeQuotation.setQuotationStatus(gmQuotationData.getStatus());
            tradeQuotation.setQuotationSerialNo(gmQuotationData.getSerial_no());
            newList.add(tradeQuotation);
        }
        boolean save = saveOrUpdateBatch(newList);
        return save;
    }

}

